package cn.temp

import android.os.Bundle
import android.text.InputType
import android.view.Gravity
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity

/**
 *
 */
abstract class BaseActivity : AppCompatActivity() {

    protected val mEt by lazy { findViewById<EditText>(R.id.et) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(layoutId)
        val ll = findViewById<LinearLayout>(R.id.ll)
        mEt.setInputType(InputType.TYPE_NULL)
        val list = arrayListOf<ButtonInfo>()
        createButton(list)
        for (info in list) {
            val btn = Button(this)
            val params = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            params.gravity = Gravity.START
            btn.layoutParams = params
            btn.text = info.name
            btn.setOnClickListener(info.onClickListener)
            ll.addView(btn)
        }
    }

    protected open val layoutId: Int
        get() = R.layout.activity_main

    abstract fun createButton(list: MutableList<ButtonInfo>)

    fun clearEt(v: View?) {
        mEt!!.setText("")
    }
}

class ButtonInfo(var name: String, var onClickListener: View.OnClickListener)
